﻿using System.Diagnostics;
using WellFormattedZhouyiGenerator;
using WellFormattedZhouyiGenerator.Extensions;
using WellFormattedZhouyiGenerator.Writers;
using YiJingFramework.Annotating.Zhouyi;
using YiJingFramework.EntityRelations.GuaCharacters.Extensions;

var uri = "https://yueyinqiu.github.io/my-yijing-annotation-stores/975345ca/2023-08-02-1.json";
var httpClient = new HttpClient();
var s = await httpClient.GetStringAsync(uri);

var output = new DirectoryInfo("./outputs");
if (output.Exists)
    output.Delete(true);
output.Create();
await output.GetFile("zhouyi.json").WriteAllTextAsync(s);

var zhouyi = ZhouyiStore.DeserializeFromJsonString(s);
Debug.Assert(zhouyi is not null);

using var writer = new CombinedWriters()
{
    Writers = new IDocumentWriter[]
    {
        new ZhihuWriter(output.GetFile("./zhihu.md"), uri)
    }
};

writer.PreWrite();

void WriteGua(int index)
{
    Debug.Assert(zhouyi is not null);
    var gua = zhouyi.GetHexagramByIndex(index.ToString("00"));
    Debug.Assert(gua is not null);
    var (upperPainting, lowerPainting) = gua.SplitToTrigrams();
    var upper = zhouyi.GetTrigram(upperPainting);
    var lower = zhouyi.GetTrigram(lowerPainting);

    var titlePart1 = $"第 {gua.Index} 卦";
    var titlePart2 = gua.Painting.ToUnicodeChar();
    var titlePart3 = gua.Name;
    var titlePart4 = upperPainting == lowerPainting ?
        $"{gua.Name}为{upper.Nature}" :
        $"{upper.Nature}{lower.Nature}{gua.Name}";
    string titlePart5 = $"{upper.Name}上{lower.Name}下";

    writer.WriteTitle($"{titlePart1} {titlePart2} {titlePart3} {titlePart4} {titlePart5}", 2);

    writer.WriteLine($"{gua.Name}：{gua.Text}");
    writer.WriteLine($"象曰：{gua.Xiang}");
    writer.WriteLine($"彖曰：{gua.Tuan}");

    foreach (var line in gua.EnumerateLines(true))
    {
        if (line.LineText is null)
            continue;

        var yinyang = line.YinYang ?? gua.FirstLine.YinYang;
        Debug.Assert(yinyang.HasValue);
        var yinyangString = yinyang.Value.IsYang ? "九" : "六";
        writer.WriteLine(line.LineIndex switch
        {
            1 => $"初{yinyangString}：{line.LineText}",
            2 => $"{yinyangString}二：{line.LineText}",
            3 => $"{yinyangString}三：{line.LineText}",
            4 => $"{yinyangString}四：{line.LineText}",
            5 => $"{yinyangString}五：{line.LineText}",
            6 => $"上{yinyangString}：{line.LineText}",
            _ => $"用{yinyangString}：{line.LineText}",
        });
        writer.WriteLine($"象曰：{line.Xiang}");
    }

    if (gua.Wenyan is not null)
        writer.WriteLine($"文言：{gua.Wenyan}");
}

writer.WriteTitle("经、象、彖、文言 上");
for (int i = 1; i <= 30; i++)
{
    WriteGua(i);
}

writer.WriteTitle("经、象、彖、文言 下");
for (int i = 31; i <= 64; i++)
{
    WriteGua(i);
}

writer.WriteTitle("系辞、说卦、序卦、杂卦");
writer.WriteTitle("系辞 上", 2);
writer.WriteLine(zhouyi.GetXici().PartA);
writer.WriteTitle("系辞 下", 2);
writer.WriteLine(zhouyi.GetXici().PartB);
writer.WriteTitle("说卦", 2);
writer.WriteLine(zhouyi.GetShuogua().Content);
writer.WriteTitle("序卦", 2);
writer.WriteLine(zhouyi.GetXugua().Content);
writer.WriteTitle("杂卦", 2);
writer.WriteLine(zhouyi.GetZagua().Content);

writer.PostWrite();

Console.WriteLine(output.FullName);
